package com.lishe.lishe.lishe.module.me.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.base.BaseApplication;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.auth.activity.AuthActivity;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.module.excitation.activity.CompanyListActivity;
import com.lishe.lishe.lishe.module.excitation.presenter.ExcitationPresenter;
import com.lishe.lishe.lishe.module.me.http.HttpUserCenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;
import com.lishe.lishe.retrofit.DataResponseListener;

import org.json.JSONObject;

/**
 * Created by zhcasecom on 2017/8/23.
 */

public class ActivitySetting extends BaseActivity implements View.OnClickListener {
    public static final int REQUEST_CODE_MODIFE_HEAD = 1001;

    private String TAG = getClass().getSimpleName();
    TextView title;
    Button Logout;

    private RelativeLayout phoneLayout;
    private RelativeLayout loadingKey;
    private RelativeLayout payKey;
    private RelativeLayout companyLayout;
    private TextView phone;
    private TextView comnpany;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        BaseApplication.getInstace().addActivity(ActivitySetting.this);
        init();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_MODIFE_HEAD) {
            }
        }
    }

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, ActivitySetting.class);
        activity.startActivity(intent);
    }

    private void init() {
        phoneLayout = findViewById(R.id.phoneLayout);
        loadingKey = findViewById(R.id.loading_key);
        payKey = findViewById(R.id.pay_key);
        companyLayout = findViewById(R.id.companyLayout);
        phone = findViewById(R.id.phone);
        comnpany = findViewById(R.id.company);
        phoneLayout.setOnClickListener(this);
        loadingKey.setOnClickListener(this);
        payKey.setOnClickListener(this);
        companyLayout.setOnClickListener(this);


        title = findViewById(R.id.title_text_title);
        title.setText("设置");

        Button back = findViewById(R.id.title_left_button);
        back.setOnClickListener(this);

        Logout = findViewById(R.id.logout);
        Logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoadingUtils.createLoadingDialog(ActivitySetting.this, "请稍等...", true);
                AuthHttpHelper.doLogout(ActivitySetting.this, logoutresponse);
            }
        });
    }


    LSWebHelper.WebCallBack response = new LSWebHelper.WebCallBack() {
        @Override
        public void onSuccess(JSONObject dataJson, String dataString, String message) {
            Log.e(TAG, message);
            String dataStr = dataString;
            JSONObject data = dataJson;
            Log.e(TAG, "data" + dataStr);
            String ph = data.optString("mobile");
            String com = data.optString("comName");
            if (ph != null) {
                phone.setText(ph);
            }
            if (com != null) {
                if(com.trim().equals("礼舍商城会员")){
                    comnpany.setText("");
                }else{
                    comnpany.setText(com);
                }
            }
        }

        @Override
        public void onFailure(String message) {

        }
    };

    LSWebHelper.WebCallBack logoutresponse = new LSWebHelper.WebCallBack() {
        @Override
        public void onSuccess(JSONObject dataJson, String dataString, String message) {

            LoadingUtils.closeDialog();
            SharedPreferencesUtils.removeToken(ActivitySetting.this);
            startActivity(new Intent(ActivitySetting.this, AuthActivity.class));
            finish();
        }

        @Override
        public void onFailure(String message) {
            LoadingUtils.closeDialog();
            Toast.makeText(ActivitySetting.this, message, Toast.LENGTH_SHORT).show();
        }
    };


    protected void onResume() {
        super.onResume();
        HttpUserCenter.getSetUserInfo(null, response);
        getComeSize();
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.title_left_button:
                finish();
                break;
            case R.id.phoneLayout:
                Log.e(TAG, "手机号码");
                break;
            case R.id.loading_key:
                Log.e(TAG, "修改密码");
                ActivityModifyPass.jump(this);
                break;
            case R.id.pay_key:
                Log.e(TAG, "支付密码");
                PayPassActivity.jump(this);
                break;
            case R.id.companyLayout:
                Log.e(TAG, "切换企业");
                CompanyListActivity.jump(ActivitySetting.this, 1);

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstace().removeActivity(ActivitySetting.this);
    }

    /**
     * 获取企业数
     */
    int comSize = 0;

    private void getComeSize() {
        comSize = 0;
        ExcitationPresenter.getInstance().getComSize(Globals.getToken(), new DataResponseListener<Integer>() {
            @Override
            public void onSuccess(Integer dataResponse) {
                SharedPreferencesUtils.saveComSize(dataResponse, ActivitySetting.this);
                comSize=dataResponse;
                if (comSize >0) {
                    companyLayout.setVisibility(View.VISIBLE);
                } else {
                    companyLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFail(String failMessage) {
                LoadingUtils.closeDialog();
                toast(failMessage);
            }

            @Override
            public void onError(Throwable e) {
                LoadingUtils.closeDialog();
                toast("网络或服务器异常！");
            }
        });
    }
}
